﻿namespace WeChatInVisualStudio
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.IO;
    using System.Reflection;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    using WindowsWeChat;


    /// <summary>
    /// Interaction logic for WeChatWindowControl.
    /// </summary>
    public partial class WeChatWindowControl : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WeChatWindowControl"/> class.
        /// </summary>
        public WeChatWindowControl()
        {
            this.InitializeComponent();
            //string path = Environment.CurrentDirectory + "//WeChatCore.dll";
            //if (File.Exists(path))
            //{
            //    Assembly viewAss = Assembly.LoadFile(path);
            //    var userControl = viewAss.CreateInstance("WindowsWeChat.MainWindow", false, BindingFlags.Default, null, null, null, null) as UserControl;
            //}
            MainWindow mainWindow = new MainWindow();
            contentControl1.Content = mainWindow;
            //CommonMethodCallBackHandlers.DownLoadQRCodeCompleted += CommonMethodCallBackHandlers_DownLoadQRCodeCompleted;
            //CommonMethodCallBackHandlers.LoginScranQRCodeCompleted += CommonMethodCallBackHandlers_LoginScranQRCodeCompleted;
            //CommonMethodCallBackHandlers.LoginCompleted += CommonMethodCallBackHandlers_LoginCompleted;
            //CommonMethodCallBackHandlers.ReceivedMsgAnalyseMsgCompleted += CommonMethodCallBackHandlers_ReceivedMsgAnalyseMsgCompleted;
            //CommonMethodCallBackHandlers.SendMsgCompleted += CommonMethodCallBackHandlers_SendMsgCompleted;
            //new Task(() =>
            //{
            //    WeiChat.InitWeiChatRobot();
            //}).Start();
        }
        void CommonMethodCallBackHandlers_SendMsgCompleted(bool obj)
        {

        }

        //void CommonMethodCallBackHandlers_ReceivedMsgAnalyseMsgCompleted(WeChatCore.Entity.MsgEntity obj)
        //{

        //}

        private void CommonMethodCallBackHandlers_LoginCompleted(bool obj)
        {
            if (obj)
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    Image1.Visibility = Visibility.Collapsed;
                    //a1.Visibility = Visibility.Visible;
                }));
            }
        }

        private void CommonMethodCallBackHandlers_LoginScranQRCodeCompleted(string obj)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                Image1.Source = new BitmapImage(new Uri(obj));
            }
            ));
        }

        private void CommonMethodCallBackHandlers_DownLoadQRCodeCompleted()
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                Image1.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\Check.jpg"));
            }));
        }

        private void a1_TextChanged(object sender, TextChangedEventArgs e)
        {
            //a1.ScrollToEnd();
        }
        ///// <summary>
        ///// Handles click on the button by displaying a message box.
        ///// </summary>
        ///// <param name="sender">The event sender.</param>
        ///// <param name="e">The event args.</param>
        //[SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions", Justification = "Sample code")]
        //[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Default event handler naming pattern")]
        //private void button1_Click(object sender, RoutedEventArgs e)
        //{
        //    MessageBox.Show(
        //        string.Format(System.Globalization.CultureInfo.CurrentUICulture, "Invoked '{0}'", this.ToString()),
        //        "WeChatWindow");
        //}
    }
}